package com.gt.self.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayOpenPublicTemplateMessageIndustryModifyRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayOpenPublicTemplateMessageIndustryModifyResponse;
import com.gt.common.AjaxResult;
import com.gt.common.AlipayConfig;
import com.gt.common.DateUtils;
import com.gt.common.PrintUtil;
import com.gt.common.QRCodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.print.PrintException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.print.PrinterException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Base64;
import java.util.Date;

@Api(tags = "测试")
@RestController
public class TestController {

    @Autowired
    private  RestTemplate restTemplate;




    @ApiOperation("测试方法1")
    @GetMapping("/test01")
    public AjaxResult test01() throws IOException {
        System.out.println("哈哈");

        QRCodeUtil qrCodeUtil = new QRCodeUtil(); http://naanhv.natappfree.cc
//        System.out.println(qrCodeUtil.createQRCode("http://5afqs2.natappfree.cc/self-machine/test02", 200, 200));
        System.out.println(qrCodeUtil.createQRCode("http://192.168.2.244:8090/self-machine/self/alipay/alipaytest", 200, 200));
        return AjaxResult.success();
    }

    @ApiOperation("测试方法2")
    @GetMapping("/test02")
    public String test02(HttpServletResponse httpResponse) throws IOException {

        Date date = DateUtils.parseDate("2024-5-21 00:00:12");

        LocalDate today = LocalDate.now();
        Date dateOnly = Date.from(today.atStartOfDay(ZoneId.systemDefault()).toInstant());
        System.out.println(dateOnly);

        if (date.getTime() < dateOnly.getTime()){
        }

        System.out.println();
        return "哎，算你支付了吧！";
    }

    @ApiOperation("测试方法3")
    @RequestMapping("/alipaytest")
    public void doPost(HttpServletRequest httpRequest,
                       HttpServletResponse httpResponse) throws ServletException, IOException, AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi-sandbox.dl.alipaydev.com/gateway.do",
                "9021000137629248",
                "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCzugiyySK4x6Xdu1SNaTYCKFf0zpXCuKTaOc2Q6cL1a9zgmMRT4nSo2TjX/cx7eiwFAXiwVk3QfgUF+IUIj+MXRteK0bLCwZbapLV3SMH54Y8sLKdGnQ3kQjZhxLKp3R21cctl80sTmDG5wW4d2sWUmBU8pY3ZsByBQEzvWUw4v4L80RQ234/rh52C8JdvOZJAb9m5ADFSW9LEKH9g6qDq1OFLb21+ORXi3czlAwVZI25M0GW/Vid91RLK9B0G7jd+TApC5x0aCvn9BJNNGol2o1KlzONwnAZjqBUWlka9sMvnCrXh1dkwI2q8nXXB0KGCzPpKVuoECAkj1bSVC6lfAgMBAAECggEBAIQ4eqm9G1RBXJ8xb9TQ8DDKKVsqmDa2zGDXYYoYz5rqDYcWHGlyEk0d8ZnFV6gHi1brbDSeaYTXES8BVC2mXXUEYxH3j8GD1k1oGzB0UHBbJo9jEnT6mfuxUny+zon06CRSYUbRpAv4TazQxwgjqHPXJtiFG0RpMa9M9MypL1AXJaFQlERUJDOV7AONThWUH/Q7T1b1mhlBGJ7wkMCjVgmnEGNYs4h5p/mwGd8gMKQZMa7exQPW1j9alzLvmDD7oIIn20/NA8WzON/P96K8e6dlR4VjNFW2G1IxZPB6oNFQ1YRTud8o6ci7q3g5zRVfpNqxvcEF1gFVazCb5oZV0eECgYEA90xkBNlRroA6UOngcF9+HcfFJ3xA1TvwepgH7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ZOaZLzKIhNI/Jae3DvIHz9DRaBsTKDh8zwIlJM3NttyTfwCVdaVpI00xyLH3x8JlNrfdXvFz4YhEoPh4TTSx8xB+ZbLYLVQ2ZlRQs4UVQRmwiRG062OOrT/FG3UkpOlr4IasYhe/UhUb4Wqp0iHhZ6RD6WC+N+VwIDAQAB",
                "RSA2");
        //获得初始化的AlipayClient
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
//        alipayRequest.setReturnUrl("http://domain.com/CallBack/return_url.jsp");
//        alipayRequest.setNotifyUrl("http://domain.com/CallBack/notify_url.jsp");//在公共参数中设置回跳和通知地址
        alipayRequest.setBizContent("{" +
                "    \"out_trade_no\":\"20221010001010100277\"," +
                "    \"total_amount\":0.1," +
                "    \"subject\":\"Iphone6 16G\"," +
                "    \"product_code\":\"QUICK_WAP_WAY\"" +
                "  }");//填充业务参数
        String form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        httpResponse.setContentType("text/html;charset=" + "UTF-8");
        httpResponse.getWriter().write(form);//直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
    }




    @ApiOperation("打印")
    @GetMapping("/testPrint")
    public AjaxResult testPrint() {

        ResponseEntity<Resource> response = restTemplate.getForEntity("http://192.168.0.252:4917/self/46677ABE-B0B2-426E-B3F3-62717DB64A1C.pdf", Resource.class);
        if (response.getStatusCode() == HttpStatus.OK) {
            Resource resource = response.getBody();
            File file = new File("D://self/321.pdf");
            try (InputStream inputStream = resource.getInputStream();
                 FileOutputStream fileOutputStream = new FileOutputStream(file)) {
                int byteRead;
                byte[] buffer = new byte[1024];
                while ((byteRead = inputStream.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, byteRead);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            //PrintUtil.PrintPdfByPath("http://192.168.0.252:4917/self/46677ABE-B0B2-426E-B3F3-62717DB64A1C.pdf");
        }
        return AjaxResult.success("打印成功！");
    }


    @ApiOperation("报道")
    @GetMapping("/testReport")
    public AjaxResult testReport() throws IOException, PrintException, PrinterException {
        PrintUtil.PrintPdfByPath("D://self\\B64823D6-12E4-42EE-81A5-451A18AED711.pdf");
        return AjaxResult.success();
    }

    @ApiOperation("支付宝支付")
    @GetMapping("/testReport1")
    public AjaxResult testReport1() throws IOException {

        return AjaxResult.success();
    }

}
